using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using QFramework;

namespace HareGame
{
    /// <summary>
    /// 硬件设备性能适配工具
    /// </summary>
    public partial class Util
    {
        public DevicePerformanceLevel Level;
        /// <summary>
        /// 获取设备性能评级
        /// </summary>
        /// <returns>性能评级</returns>
        public DevicePerformanceLevel GetDevicePerformanceLevel()
        {
            LogKit.I("显卡ID:" + SystemInfo.graphicsDeviceVendorID);
            LogKit.I("显卡名称:" + SystemInfo.graphicsDeviceName);
            LogKit.I("显卡类型:" + SystemInfo.graphicsDeviceType);
            LogKit.I("支持渲染多少目标纹理:" + SystemInfo.supportedRenderTargetCount);
            LogKit.I("是否支持2D数组纹理:" + SystemInfo.supports2DArrayTextures);
            LogKit.I("是否支持3D（体积）纹理:" + SystemInfo.supports3DTextures);
            LogKit.I("是否支持计算着色器:" + SystemInfo.supportsComputeShaders);
            LogKit.I("是否支持阴影深度:" + SystemInfo.supportsRawShadowDepthSampling);
            LogKit.I("是否支持实例化GPU的Draw Call:" + SystemInfo.supportsInstancing);
            LogKit.I("是否支持内置阴影:" + SystemInfo.supportsShadows);
            LogKit.I("是否支持稀疏纹理:" + SystemInfo.supportsSparseTextures);
            if (SystemInfo.graphicsDeviceVendorID == 32902)
            {
                //集显
                return DevicePerformanceLevel.Low;
            }
            else //NVIDIA系列显卡（N卡）和AMD系列显卡
            {
                //根据目前硬件配置三个平台设置了不一样的评判标准
                //CPU核心数
                LogKit.I("CPU核心数:" + SystemInfo.processorCount);
                LogKit.I("CPU主频:" + SystemInfo.processorFrequency);
                LogKit.I("CPU名称:" + SystemInfo.processorType);
#if UNITY_EDITOR || UNITY_STANDALONE_WIN
                if (SystemInfo.processorCount <= 2)
#elif UNITY_STANDALONE_OSX || UNITY_IPHONE
            if (SystemInfo.processorCount < 2)
#elif UNITY_ANDROID
            if (SystemInfo.processorCount <= 4)
#endif
                {
                    //CPU核心数<=2判定为低端
                    return DevicePerformanceLevel.Low;
                }
                else
                {
                    //显存
                    LogKit.I("显存:" + SystemInfo.graphicsMemorySize);
                    int graphicsMemorySize = SystemInfo.graphicsMemorySize;

                    //内存
                    LogKit.I("内存:" + SystemInfo.systemMemorySize);
                    int systemMemorySize = SystemInfo.systemMemorySize;
                    if (graphicsMemorySize >= 4096 && systemMemorySize >= 8192)
                        return DevicePerformanceLevel.High;
                    else if (graphicsMemorySize >= 2048 && systemMemorySize >= 4096)
                        return DevicePerformanceLevel.Mid;
                    else
                        return DevicePerformanceLevel.Low;
                }
            }
        }

        /// <summary>
        /// 根据机型配置自动设置质量
        /// </summary>
        public void ModifySettingsBasedOnPerformance()
        {
            Level = GetDevicePerformanceLevel();
            LogKit.I("最终设备评级:" + Level);
            switch (Level)
            {
                case DevicePerformanceLevel.Low:
                    SetQualitySettings(QualityLevel.Low);
                    break;
                case DevicePerformanceLevel.Mid:
                    SetQualitySettings(QualityLevel.Mid);
                    break;
                case DevicePerformanceLevel.High:
                    SetQualitySettings(QualityLevel.High);
                    break;
            }
        }

        /// <summary>
        /// 根据自身需要调整各级别需要修改的设置，可根据需求修改低中高三种方案某一项具体设置
        /// </summary>
        /// <param name="qualityLevel">质量等级</param>
        public void SetQualitySettings(QualityLevel qualityLevel)
        {
            switch (qualityLevel)
            {
                case QualityLevel.Low:
                    //前向渲染使用的像素灯的最大数量，建议最少为1
                    QualitySettings.pixelLightCount = 2;
                    //设置抗锯齿级别。选项有​​ 0_不开启抗锯齿，2_2倍，4_4倍和8_8倍采样。
                    QualitySettings.antiAliasing = 0;
                    //设置软硬阴影是否打开
                    QualitySettings.shadows = ShadowQuality.Disable;
                    //设置垂直同步方案，VSyncs数值需要在每帧之间传递，使用0为不等待垂直同步。值必须是0，1或2。
                    QualitySettings.vSyncCount = 0;
                    break;
                case QualityLevel.Mid:
                    QualitySettings.pixelLightCount = 2;
                    QualitySettings.antiAliasing = 2;
                    QualitySettings.shadows = ShadowQuality.HardOnly;
                    QualitySettings.shadowResolution = ShadowResolution.Medium;
                    QualitySettings.vSyncCount = 0;
                    break;
                case QualityLevel.High:
                    QualitySettings.pixelLightCount = 4;
                    QualitySettings.antiAliasing = 8;
                    QualitySettings.shadows = ShadowQuality.All;
                    QualitySettings.shadowResolution = ShadowResolution.VeryHigh;
                    break;
            }
        }
    }
    public enum DevicePerformanceLevel
    {
        Low,
        Mid,
        High
    }
    public enum QualityLevel
    {
        Low,
        Mid,
        High
    }
}